<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c"%>
<% String path = request.getContextPath();%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>营销模板 新增</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body>
<form id="mcrFormId">
    <div class="fieldset1 tabel_col tabel_col_s clearFix noBorder">

        <div class="fieldBox col_50">
            <div class="label">描述</div>
            <div class="field">
                <input name="content" id="content" type="text" class="ui-textbox" value="" validate="{required:true, minlength:2, maxlength:50}">
                <span class="requiredSymbol">*</span>
            </div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">模板类型</div>
            <div class="field">
                <select name="tempType" id="tempType">
                    <option value="1">微信</option>
                </select>
            </div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">上|中|下图片上传</div>
            <div class="field">
                <jsp:include page="../../system/sysUlFilesRec/sysSysUlFilesRecUpload.jsp" flush="true">
                    <jsp:param name="fileNum" value="3"/>
                    <jsp:param name="filePath" value="mcrCustomer/temp/"/>
                </jsp:include>
            </div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">背景颜色</div>
            <div class="field">
                <input name="background" id="background" type="color" class="ui-textbox" value="" validate="{required:true}">
            </div>
        </div>

        <div class="fieldBox col_50">
            <div class="label">是否启用</div>
            <div class="field">
                <select name="isValid" id="isValid">
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>

        <!-- hide data -->
        <div class="fieldBox col_100" style="display:none;">
            <div class="field">
                <input name="dataId" id="dataId" type="text" class="ui-textbox" value="${id}">
            </div>
        </div>

    </div>
</form>

<script>
    $(function() {
        $("#mcrFormId").ligerForm({
            validate: true
        });
        initData();
    });

    //定义父级调用事件
    frameElement.dialog.onOk = function () {
        save();
    }

    frameElement.dialog.onCancel = function () {
        frameElement.dialog.close();
    }

    //保存
    function save(){

        //获取上传文件地址
        var fileParams = getUploadResultParams();
        console.log("上传 fileParams >>> " + JSON.stringify(fileParams));

        var ligerForm = liger.get("mcrFormId");
        var loanId = $("#loanId").val();
        if (!ligerForm.valid()) {
            UIUtils.alertWarning("有验证信息未通过!");
            return;
        }
        var data = ligerForm.getData();
        data["fileParams"]= JSON.stringify(fileParams);
        console.log("表单 data >>> " + JSON.stringify(data));

        var dataId = $("#dataId").val();
        var url = '<%=path%>/mcr/mcrMarketTemplet/add.do';
        if(StringUtils.isNotEmpty(dataId)){
            data["id"]= dataId;
            url = '<%=path%>/mcr/mcrMarketTemplet/modify.do';
        }
        AjaxUtils.ajaxPost(url, data, addCallback);
    }


    //新增回调
    function addCallback(msg){
        if(msg.success == 1){
            parent.grid.reload();
            UIUtils.alertSuccess("操作成功!");
            frameElement.dialog.close();
        }else{
            UIUtils.alertError(msg.info);
        }
    }

    //修改初始化数据
    function initData(){
        var dataId = $("#dataId").val();
        if(StringUtils.isEmpty(dataId))return;
        var _url = '<%=path%>/mcr/mcrMarketTemplet/getById.do';
        AjaxUtils.ajaxGet(_url, {id:dataId}, initCallback);
    }

    //初始化回调
    function initCallback(msg){
        if(msg.success == 1){
            setData(msg.data);//设置数据
        }else{
            UIUtils.alertError(msg.info);
        }
    }

    //修改时候设置页面数据
    function setData(data){
        var form = new liger.get("mcrFormId");
        form.setData({
            content:data.content,
            tempType:data.tempType,
            topImgPath:data.topImgPath,
            midImgPath:data.midImgPath,
            botImgPath:data.botImgPath,
            background:data.background,
            isValid:data.isValid
        });
    };

</script>

</body>
</html>